/**
 *
 */
package com.angel.dao.generic.jdbc.info;

import com.angel.common.helpers.StringHelper;

/**
 * @author William
 *
 */
public class JDBCConnectionInfo {

	private static final String DATA_SOURCE_NAME = "dataSourceName";
	private static final String JDBC_PREFIX = "jdbc";

	private String jdbcDriver;

	private String engine;

	private String serverIP;

	private String serverPort;

	private String schema;

	private boolean autoCommit = true;

	private boolean readOnly = false;

	private int transactionIsolation;

	private String userName;

	private String password;

	private String dataSourceName = DATA_SOURCE_NAME;

	/** Connection uri format: 	"jdbc:mysql://localhost:3306/initializeData".
	 *
	 * @param jdbcDriver
	 * @param connectionURI
	 */
	public JDBCConnectionInfo(String jdbcDriver, String connectionURI) {
		super();
		this.jdbcDriver = jdbcDriver;
		this.fillConnectionInformation(connectionURI);
	}

	private void fillConnectionInformation(String connectionURI) {

		if (connectionURI.startsWith(JDBC_PREFIX)) {

			String[] splits = connectionURI.split(":");
			this.engine = splits[1];
			if (splits != null && splits.length == 4) {
				//"jdbc:mysql://localhost:3306/initializeData"
				this.serverIP = splits[2].substring(2);
				this.serverPort = splits[3]
						.substring(0, splits[3].indexOf("/"));
				this.schema = splits[3].substring(splits[3].indexOf("/") + 1,
						splits[3].length());
			} else {
				//"jdbc:mysql://localhost/initializeData"
				this.serverIP = splits[2].substring(2).substring(0,
						splits[2].substring(2).indexOf("/"));
				this.serverPort = "";
				this.schema = splits[2].substring(2).substring(
						splits[2].substring(2).indexOf("/") + 1,
						splits[2].substring(2).length());
			}
		} else {
			String[] splits = connectionURI.split(":");
			this.engine = splits[0];
			if (splits != null && splits.length == 3) {
				//"mysql://localhost:3306/initializeData"
				this.serverIP = splits[1].substring(2);
				this.serverPort = splits[2]
						.substring(0, splits[2].indexOf("/"));
				this.schema = splits[2].substring(splits[2].indexOf("/") + 1,
						splits[2].length());
			} else {
				//"mysql://localhost/initializeData"
				this.serverIP = splits[1].substring(2).substring(0,
						splits[1].substring(2).indexOf("/"));
				this.serverPort = "";
				this.schema = splits[1].substring(2).substring(
						splits[1].substring(2).indexOf("/") + 1,
						splits[1].substring(2).length());
			}
		}
	}

	public JDBCConnectionInfo(String jdbcDriver) {
		super();
		this.jdbcDriver = jdbcDriver;
	}

	/**
	 * @return the jdbcDriver
	 */
	public String getJdbcDriver() {
		return jdbcDriver;
	}

	/**
	 * @param jdbcDriver the jdbcDriver to set
	 */
	public void setJdbcDriver(String jdbcDriver) {
		this.jdbcDriver = jdbcDriver;
	}

	/**
	 * @return the engine
	 */
	public String getEngine() {
		return engine;
	}

	/**
	 * @param engine the engine to set
	 */
	public void setEngine(String engine) {
		this.engine = engine;
	}

	/**
	 * @return the serverIP
	 */
	public String getServerIP() {
		return serverIP;
	}

	/**
	 * @param serverIP the serverIP to set
	 */
	public void setServerIP(String serverIP) {
		this.serverIP = serverIP;
	}

	/**
	 * @return the serverPort
	 */
	public String getServerPort() {
		return serverPort;
	}

	/**
	 * @param serverPort the serverPort to set
	 */
	public void setServerPort(String serverPort) {
		this.serverPort = serverPort;
	}

	/**
	 * @return the schema
	 */
	public String getSchema() {
		return schema;
	}

	/**
	 * @param schema the schema to set
	 */
	public void setSchema(String schema) {
		this.schema = schema;
	}

	/**
	 * @return the connectionURI
	 */
	public String getConnectionURI() {
		String serverPort = StringHelper.isEmpty(this.getServerPort()) ? ""
				: ":" + this.getServerPort();
		return JDBC_PREFIX + ":" + this.getEngine() + "://"
				+ this.getServerIP() + serverPort + "/" + this.getSchema();
	}

	/**
	 * @return the autoCommit
	 */
	public boolean isAutoCommit() {
		return autoCommit;
	}

	/**
	 * @param autoCommit the autoCommit to set
	 */
	public void setAutoCommit(boolean autoCommit) {
		this.autoCommit = autoCommit;
	}

	/**
	 * @return the transactionIsolation
	 */
	public int getTransactionIsolation() {
		return transactionIsolation;
	}

	/**
	 * @param transactionIsolation the transactionIsolation to set
	 */
	public void setTransactionIsolation(int transactionIsolation) {
		this.transactionIsolation = transactionIsolation;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the readOnly
	 */
	public boolean isReadOnly() {
		return readOnly;
	}

	/**
	 * @param readOnly the readOnly to set
	 */
	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}

	/**
	 * @return the dataSourceName
	 */
	public String getDataSourceName() {
		return dataSourceName;
	}

	/**
	 * @param dataSourceName the dataSourceName to set
	 */
	public void setDataSourceName(String dataSourceName) {
		this.dataSourceName = dataSourceName;
	}
}
